package reso.examples.gobackn.gui;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import reso.examples.gobackn.transport.DummyProtocol;

public class GuiJfreeChartModel {
	private static final Logger LOGGER = LogManager
			.getLogger(GuiJfreeChartModel.class);

	public enum Operation {
		SLOW_START, CONGESTION_AVOIDANCE, FAST_RECOVERY, TIMEOUT
	}

	private int counterRtt = 0;

	private double ssthresh;
	private double cwnd;
	private Operation currentOperation = Operation.SLOW_START;

	private List<ChangeListener> listeners = new ArrayList<ChangeListener>();
	private boolean isFinnished;

	public void addListener(ChangeListener listener) {
		listeners.add(listener);
	}

	public synchronized void setSsthreshAndCwnd(Operation operation,
			double ssthresh, double cwnd, boolean isFinnished) {
		LOGGER.info("CHANGE - operation=" + operation + " ; ssthresh=" + ssthresh
				+ " ; cwnd=" + cwnd+"; "+isFinnished);
		this.ssthresh = ssthresh;
		this.cwnd = cwnd;
		this.currentOperation = operation;
		counterRtt++;
		this.isFinnished = isFinnished;
		this.performEvent();
	}

	public double getSsthresh() {
		return ssthresh;
	}

	public double getCwnd() {
		return cwnd;
	}

	public int getCounterRtt() {
		return counterRtt;
	}

	public boolean isFinnished() {
		return isFinnished;
	}

	public boolean setIsFinnished(boolean isFinished) {
		return isFinnished;
	}

	public Operation getCurrentOperation() {
		return currentOperation;
	}

	private void performEvent() {
		for (ChangeListener listener : this.listeners) {
			listener.stateChanged(new ChangeEvent(this));
		}
	}

}
